import Vue from 'vue'
import Vuex from 'vuex'
import { Toast } from 'vant';
import createLogger from 'vuex/dist/logger'
//导入持久化插件
import createPersistedState from "vuex-persistedstate";
Vue.use(Vuex)

import { shop_favlist, shop_fav, shop_unfav, shopcart_list, shopcart_clear, shopcart_add, shopcart_sub, address_list, address_add, address_edit, address_del } from '../utils/api'
export default new Vuex.Store({
  state: {
    collectList: [],
    userid: 0,
    shop_cart_list: [],
    addresslist: [],
    address_info: [],
    historylist: [],
    payorderlist: []
  },
  getters: {
    totalPrice(state) {
      let sum = 0;
      state.shop_cart_list.forEach((element) => {
        sum += element.min_price * element.count;
      })
      sum = sum.toFixed(2)
      console.log(sum)
      return sum
    },
    totalNum(state) {
      let sum = 0;
      state.shop_cart_list.forEach((element) => {
        sum += element.count;
      })
      return sum
    }
  },
  mutations: {
    //保存搜索历史记录
    history(state, payload) {
      let index = state.historylist.findIndex((element) => {
        return element == payload
      })
      console.log(index)
      if (index == -1) {
        state.historylist.push(payload)
      }

    },
    //生成待支付订单
    save_paylist(state) {
      state.payorderlist = state.shop_cart_list
      state.shop_cart_list = []
    },
    //清空历史记录
    cleanhistory(state, payload) {
      state.historylist = [];
    },
    //收藏一个店铺
    collect(state, payload) {
      Toast.success('已收藏');
      state.collectList.push(payload);
    },
    //取消收藏一个店铺
    cancel_collect(state, payload) {
      Toast.fail('已取消收藏');
      var index = state.collectList.findIndex((item) => {
        return item.mtWmPoiId == payload.mtWmPoiId
      })
      if (index != -1) {
        state.collectList.splice(index, 1)
      }
    },
    add_userid(state, payload) {
      state.userid = payload
    },
    //保存收藏列表
    save_favlist(state, payload) {
      state.collectList = payload
    },
    // 保存购物车列表
    save_shopcartlist(state, payload) {
      state.shop_cart_list = payload
    },
    //清空购物车
    clearshopcart(state) {
      state.shop_cart_list = []
    },
    //数量加1
    addshopcart(state, payload) {
      console.log(payload)
      let index = state.shop_cart_list.findIndex((element) => {
        return element.id == payload.id
      });
      if (index == -1) {
        let good = JSON.parse(JSON.stringify(payload));
        good.count = 1;
        state.shop_cart_list.push(good)
      } else {
        state.shop_cart_list[index].count++
      }
    },
    //数量减1
    redshopcart(state, payload) {
      let index = state.shop_cart_list.findIndex((element) => {
        return element.id == payload.id
      });
      console.log(state.shop_cart_list[index])
      if (index != -1) {
        state.shop_cart_list[index].count--
      };
      if (state.shop_cart_list[index].count == 0) {
        state.shop_cart_list.splice(index, 1)
      }

    },
    //保存收货地址
    save_addresslist(state, payload) {
      state.addresslist = payload
    },
    //保存addressinfo
    save_addressinfo(state, payload) {
      state.address_info.push(payload);
    },
    //删除收货地址
    del_address(state, payload) {
      let index = state.addresslist.findIndex((element) => {
        return element.id == payload
      })
      if (index != -1) {
        state.addresslist.splice(index, 1)
      }

    }
  },
  actions: {
    //获取店铺列表
    request_favlist(store) {
      let userid = JSON.parse(localStorage.getItem('userinfo')).id
      shop_favlist({ userid }).then((res) => {
        store.commit('save_favlist', res.data.list)
      })
    },
    //收藏一个店铺
    request_fav(store, payload) {
      let userid = store.state.userid
      console.log(payload)
      let shopid = payload.mtWmPoiId
      shop_fav({ userid, shopid }).then((res) => {
        store.commit('collect', payload)
      })
    },
    // 取消收藏一个店铺
    request_unfav(store, payload) {
      let userid = store.state.userid
      let shopid = payload.mtWmPoiId
      shop_unfav({ userid, shopid }).then((res) => {
        store.commit('cancel_collect', payload)
      })
    },
    //获取当前购物车列表
    request_shopcartlist(store) {
      let userid = JSON.parse(localStorage.getItem('userinfo')).id
      shopcart_list({ userid }).then((res) => {
        if (res.data.code == 200) {
          store.commit('save_shopcartlist', res.data.list)
        }
      })
    },
    //清空购物车列表
    request_clearshopcart(store) {
      let userid = store.state.userid
      shopcart_clear({ userid }).then((res) => {
        if (res.data.code == 200) {
          store.commit('clearshopcart')
        }
      })
    },
    //数量加1
    request_addshopcart(store, payload) {
      let userid = store.state.userid
      let goodid = payload.id
      shopcart_add({ userid, goodid }).then((res) => {
        if (res.data.code == 200) {
          store.commit('addshopcart', payload)
        }
      })
    },
    //数量减1
    request_redshopcart(store, payload) {
      let userid = store.state.userid
      let goodid = payload.id
      shopcart_sub({ userid, goodid }).then((res) => {
        if (res.data.code == 200) {
          store.commit('redshopcart', payload)
        }
      })
    },

    //请求收货地址列表
    request_addresslist(store, payload) {
      let userid = store.state.userid
      address_list({ userid }).then((res) => {
        if (res.data.code == 200) {
          store.commit('save_addresslist', res.data.list)
        }
      })
    },
    //请求新建一个收货地址
    request_address_add(store, payload) {
      let userid = store.state.userid
      let address = payload.province + payload.city + payload.county + payload.addressDetail
      let phone = payload.tel
      let name = payload.name
      store.commit('save_addressinfo', payload)
      address_add({ userid, address, phone, name }).then((res) => {
        if (res.data.code == 200) {
        }
      })
      //更新服务器和vuex地址列表
      address_list({ userid }).then((res) => {
        if (res.data.code == 200) {
          store.commit('save_addresslist', res.data.list)
        }
      })
    },
    //请求修改地址
    request_address_edit(store, payload) {
      let userid = store.state.userid
      let address = payload.province + payload.city + payload.county + payload.addressDetail
      let phone = payload.tel
      let addressid = payload.id
      let name = payload.name
      address_edit({ userid, addressid, address, phone, name }).then((res) => {
        if (res.data.code == 200) {
          console.log('修改成功')
        }
      })
      //更新服务器和vuex地址列表
      address_list({ userid }).then((res) => {
        if (res.data.code == 200) {
          store.commit('save_addresslist', res.data.list)
        }
      })

    },
    //请求删除地址
    request_address_del(store, payload) {
      let userid = store.state.userid;
      let addressid = payload;
      address_del({ userid, addressid }).then((res) => {
        if (res.data.code == 200) {
          store.commit('del_address', payload)
        }
      })
    }


  },
  modules: {
  },
  plugins: [createLogger(), createPersistedState()]
})
